package application;
	
import java.util.function.Consumer;

import com.sun.glass.ui.Robot;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Duration;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;


public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		    ConnectionDanMu conn=new ConnectionDanMu();
		    conn.setRequest_rul("https://api.live.bilibili.com/ajax/msg");
		    Button button=new Button("获取弹幕");
		    Button clsoe=new Button("断开链接");
		    TextField field=new TextField();
		    field.setPromptText("请输入B站直播房间号");
		    TableView<DanmuData> tableview=new TableView<DanmuData>();
		    //每一列宽度一样
		    tableview.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
		    //可以拣选
		    tableview.setTableMenuButtonVisible(true);
		    
		    TableColumn<DanmuData, String> name=new TableColumn<DanmuData, String>("姓名");
		    TableColumn<DanmuData, String> text=new TableColumn<DanmuData, String>("弹幕");
		    TableColumn<DanmuData, String> time=new TableColumn<DanmuData, String>("时间");
		    name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<DanmuData,String>, ObservableValue<String>>() {
				
				@Override
				public ObservableValue<String> call(CellDataFeatures<DanmuData, String> arg0) {
					SimpleStringProperty property=new SimpleStringProperty(arg0.getValue().getNickename());
					return property;
				}
			});
		    text.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<DanmuData,String>, ObservableValue<String>>() {
				
				@Override
				public ObservableValue<String> call(CellDataFeatures<DanmuData, String> arg0) {
					SimpleStringProperty property=new SimpleStringProperty(arg0.getValue().getText());
					return property;
				}
			});
		    time.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<DanmuData,String>, ObservableValue<String>>() {
		
		       @Override
		       public ObservableValue<String> call(CellDataFeatures<DanmuData, String> arg0) {
			       SimpleStringProperty property=new SimpleStringProperty(arg0.getValue().getTimeline());
			       return property;
		        }
	        });
		    tableview.getColumns().add(name);
		    tableview.getColumns().add(text);
		    tableview.getColumns().add(time);
		  
		    VBox box=new VBox();
		    HBox box1=new HBox(10);
		    
		    box.setStyle("-fx-background-color:#ffffff");
		  
		    box1.setPrefHeight(40);
		    box1.setAlignment(Pos.CENTER);
		    VBox.setVgrow(tableview, Priority.ALWAYS);
		    
		    box1.getChildren().addAll(button,field,clsoe);
		    box.getChildren().addAll(box1,tableview);
			Scene scene = new Scene(box);
			primaryStage.setHeight(800);
			primaryStage.setWidth(800);
			primaryStage.setTitle("b站直播弹幕抓取助手");
			primaryStage.setScene(scene);
			primaryStage.show();
			primaryStage.setOpacity(0.8);
			DanMuTask danMuTask=new DanMuTask(conn);
			danMuTask.valueProperty().addListener(new ChangeListener<ObservableList<DanmuData>>() {

				@Override
				public void changed(ObservableValue<? extends ObservableList<DanmuData>> observable,
						ObservableList<DanmuData> oldValue, ObservableList<DanmuData> newValue) {
					if(newValue!=null) {
						tableview.setItems(newValue);
						//滚动最下面
						tableview.scrollTo(newValue.size()-1);
					}
					
				}
			});
			//置顶
			primaryStage.setAlwaysOnTop(true);
			button.setOnAction(new EventHandler<ActionEvent>() {
				
				@Override
				public void handle(ActionEvent arg0) {
					if(field.equals("")==false) {
						conn.setRequest_roomid(field.getText());
						//几秒启动
						danMuTask.setDelay(Duration.seconds(0));
						//几秒运行一次
						danMuTask.setPeriod(Duration.seconds(1));
						danMuTask.start();
						//不断开不能改数据
						button.setDisable(true);
						field.setDisable(true);
					}
					
				}
			});
			clsoe.setOnAction(new EventHandler<ActionEvent>() {
				
				@Override
				public void handle(ActionEvent arg0) {
					conn.disconnect();
					danMuTask.cancel();//取消
					danMuTask.reset();//重置
					
					button.setDisable(false);
					field.setDisable(false);
					
				}
			});
			
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}
